#!/usr/bin/env python
# -*- coding: utf-8 -*-
# author： 胡浩浩
# datetime： 2021/12/29 15:08 
# ide： PyCharm

from typing import List
from fastapi import APIRouter, Depends, HTTPException
from sqlalchemy.orm import Session
from Models.environmentModel import environment_model
from Tools.database import engine
from Dependencies.db_depedencies import get_db
from Controllers.environmentController import environment_controller
from Schemas.environmentSchema import environment_schema

environment_model.Base.metadata.create_all(bind=engine)
router = APIRouter()


@router.get("/environment/get/list", response_model=List[environment_schema.Environment])
async def read_environment_list(skip: int = 0, limit: int = 10, db: Session = Depends(get_db)):
    environment_list = environment_controller.get_environment_list(db=db, skip=skip, limit=limit)
    return environment_list


@router.get("/environment/get/{environment_id}", response_model=environment_schema.Environment)
async def read_environment_by_id(environment_id: int, db: Session = Depends(get_db)):
    db_environment = environment_controller.get_environment_by_id(db=db, environment_id=environment_id)
    if db_environment is None:
        raise HTTPException(status_code=404, detail="environment not fund")
    return db_environment


@router.post("/environment/create", response_model=environment_schema.Environment)
async def create_environment(environment: environment_schema.EnvironmentCreate, db: Session = Depends(get_db)):
    db_environment = environment_controller.create_environment(db=db, environment=environment)
    return db_environment
